package com.moge.anjian.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;

import com.moge.anjian.R;
import com.moge.anjian.utils.PageUtils;
import com.moge.anjian.utils.PermissionUtil;
import com.moge.anjian.manager.TargetManager;
import com.moge.anjian.view.AnjianHatchView;

public class MainActivity extends ComponentActivity {
    Button clickBtn, goSettingBtn, goAccSettingBtn, goAnjianRuleBtn, goOverDrawBtn, goTestPageBtn,
            goRecordBtn, goPlaybackBtn;
    AnjianHatchView hatchView;
    WindowManager manager;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

        clickBtn = findViewById(R.id.btn_click);
        goSettingBtn = findViewById(R.id.btn_go_setting);
        goAccSettingBtn = findViewById(R.id.btn_go_acc_setting);
        goOverDrawBtn = findViewById(R.id.btn_go_overdraw);
        goAnjianRuleBtn = findViewById(R.id.btn_go_anjian_rule);
        goTestPageBtn = findViewById(R.id.btn_go_test_page);
        goRecordBtn = findViewById(R.id.btn_go_record);
        goPlaybackBtn = findViewById(R.id.btn_go_playback);
        //进入设置页面
        goSettingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PermissionUtil.isAccessibilityServiceEnabled()) {
                    goAccSettingBtn.setText(R.string.btn_go_setting_enable_text);
                    Toast.makeText(MainActivity.this, R.string.btn_go_setting_enable_text, Toast.LENGTH_SHORT).show();
                } else {
                    PageUtils.goAppDetailPage(MainActivity.this);
                }
            }
        });

        //进入无障碍服务设置页面
        goAccSettingBtn.setOnClickListener(v -> PageUtils.goAccessibilityPage(MainActivity.this));

        goOverDrawBtn.setOnClickListener(v -> PageUtils.goOverDraw(MainActivity.this));

        goAnjianRuleBtn.setOnClickListener(v -> PageUtils.goRulePage(MainActivity.this));

        clickBtn.setOnClickListener(v -> {
            if (PermissionUtil.isAccessibilityServiceEnabled()) {
                showFloatWindow();
            }
        });

        goTestPageBtn.setOnClickListener(v -> PageUtils.goTestPage(MainActivity.this));

        goRecordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PermissionUtil.isAccessibilityServiceEnabled()
                        && PermissionUtil.isDrawOverlay(MainActivity.this.getApplicationContext())) {
                    // 开始录制
                    TargetManager.getInstance().cleanWindow();
                    PageUtils.goHomeScreen(MainActivity.this);

                }
            }
        });
        goPlaybackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PageUtils.goPlayBack(MainActivity.this);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (PermissionUtil.isAccessibilityServiceEnabled()) {
            goAccSettingBtn.setText(R.string.btn_go_setting_enable_text);
        } else {
            goAccSettingBtn.setText(R.string.btn_go_setting_text);
        }

        if (PermissionUtil.isDrawOverlay(this.getApplicationContext())) {
            goOverDrawBtn.setText(R.string.btn_go_overdraw_enable_text);
        } else {
            goOverDrawBtn.setText(R.string.btn_go_overdraw_text);
        }
    }

    private void showFloatWindow() {
        // 权限判断
        if (PermissionUtil.isDrawOverlay(this.getApplicationContext())) {
            // 已经有权限了,就去初始化对应的视图或者悬浮窗弹窗的初始化
            if (hatchView == null) {
                hatchView = new AnjianHatchView(MainActivity.this);
            }
            if (hatchView.isAttachedToWindow()) {
                hatchView.removeWindow();
            }
            Log.e("dimoge", "添加一个悬浮窗");
            hatchView.addWindow();
        } else {
            Log.e("dimoge", "请检查悬浮窗权限");
        }
    }
}
